<?global
	
#include "cpp/index_thread.h"

?><?c

	// Start the indexer thread if we got root
	str::t_string sRoot = in[ "GET" ][ "root" ].str();
	
	if ( sRoot.length() )
	{
		// Ensure valid drive
		t_pb job;
		if ( disk::GetDiskInfo( job[ "drive" ], sRoot ) )
		{	
			// Set the job information
			job[ "params" ] = in[ "GET" ];
			
			// Run the new job
			job[ "run" ] = 1;
			
			// Cancel any previous job
			job[ "cancel" ] = 1;

			// Set the job info
			tq::set( "indexer.job", job, "." );
			
			// Ensure indexer thread is running
			tq::start( "indexer", index_thread );

		} // end if

	} // end if

?>
<body>
    <div id="chart"></div>
	<div class='rightoptions'>
		<label for='maxtop'>Top</label>
		<select id='maxtop' class='seloption'>
			<?c
				for( long i = 5; i <= 10; i++ )
					out << "<option value='" 
						<< str::ToString< str::t_string8 >( i ) 
						<< ( ( i == 5 ) ? "' selected>" : "'>" )
						<< str::ToString< str::t_string8 >( i ) 
						<< "</option>";
			?>
		</select>
		<label for='maxdepth'>Depth</label>
		<select id='maxdepth' class='seloption'>
			<?c
				for( long i = 1; i <= 5; i++ )
					out << "<option value='" 
						<< str::ToString< str::t_string8 >( i ) 
						<< ( ( i == 3 ) ? "' selected>" : "'>" )
						<< str::ToString< str::t_string8 >( i ) 
						<< "</option>";
			?>
		</select>
	</div>
	<script type="text/javascript">

		var w = 640,
			h = 480,
			r = Math.min(w, h) / 2,
			color = d3.scale.category20c();

		var vis = d3.select("#chart").append("svg")
			.attr("width", w)
			.attr("height", h)
			.append("g")
			.attr("transform", "translate(" + w / 2 + "," + h / 2 + ")");

		var partition = d3.layout.partition()
			.sort(null)
			.size([2 * Math.PI, r * r])
			.value(function(d) { return d.size; });

		var arc = d3.svg.arc()
			.startAngle(function(d) { return d.x; })
			.endAngle(function(d) { return d.x + d.dx; })
			.innerRadius(function(d) { return Math.sqrt(d.y); })
			.outerRadius(function(d) { return Math.sqrt(d.y + d.dy); });

		function notzero( a )
		{	for ( var i in a )
				return true;
			return false;
		}

		function poll_data()
		{
			if ( getPage() != 'graph' )
				return;
		
			$.ajax({ url: "data.htm", 
					 dataType: "json",
					 data:  { 	
								center: $('.centeritem').html(),
								top:	$('#maxtop').val(),
								depth:	$('#maxdepth').val()
							}
							,
					 success: function(json) 
					 {	
						if ( json.drive )
							setProgress( '#diskprogress', json.drive.progress, json.drive.bytes_used );
					 
						if ( json.map && notzero( json.map ) )
						{
							var path = vis
							   .data([json.map])
							   .selectAll("path")
							   .data(partition.nodes)
							   .attr("d", arc)
							   .style("stroke", "#222")
							   .style("fill", function(d) 
							   { 	if ( d.path == $('.highlight').html() )
									{	$('.highlight').html( d.path );
										$('.hdr_size').html( d.szstr );
										return d.hi_colour;
									} // end if
									else
										return d.colour; 
							   })
								;

							path
							   .enter().append("path")
//							   .attr("display", function(d) { return d.depth ? null : "none"; })
							   .attr("d", arc)
							   .style("stroke", "#222")
							   .style("fill", function(d) { return d.colour; })
							   .on("mouseover", function(d)
							   {	$('.highlight').html( d.path );
									$('.hdr_size').html( d.szstr );
									d3.select(this).style("fill", d.hi_colour);
							   })
							   .on("mouseout", function(d)
							   {	$('.selitem').html( '' );
									d3.select(this).style("fill", d.colour);
							   })
							   .on("mousedown", function(d)
							   {	if ( d.dir )
							   			$('.centeritem').html( d.dst ? d.dst : ( d.depth ? d.path : '' ) ),
										$('.highlight').html( d.dst ? d.dst : ( d.depth ? d.path : '' ) );
							   })
							   ;

							path
								.exit()
								.remove()
								;

						} // end if

						setTimeout( poll_data, 500 );
					 },
					 error: function(xhr,opt,msg) { setTimeout( poll_data, 1000 ); },
				});
		}

		poll_data();

	</script>
</body>

